package com.sharemarking.application.common.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sharemarking.application.common.BaseSpecification;
import com.sharemarking.application.common.entity.Department;
import com.sharemarking.application.common.entity.Dict;
import com.sharemarking.application.common.entity.Employee;
import com.sharemarking.application.common.entity.Leave;
import com.sharemarking.application.common.repository.DepartmentRepo;
import com.sharemarking.application.common.repository.DictRepo;
import com.sharemarking.application.common.repository.EmployeeRepo;
import com.sharemarking.application.common.repository.LeaveRepo;

@Controller
@RequestMapping("leave")
public class LeaveController {

	@Autowired
	private LeaveRepo lr;
	
	@Autowired
	private EmployeeRepo er;
	
	@Autowired
	private DepartmentRepo dr;
	
	@Autowired
	private DictRepo dr1;
	
	
	/**
	 * 请假数据列表
	 * 
	 * @param e
	 * @param page
	 * @param size
	 * @param model
	 */
	@GetMapping(value ="index")
	@PreAuthorize("hasPermission('sys:leave',1)")
	public void index(Leave e, Integer page, Integer size, Model model) {
		if (page == null) {
			page = 0;
		} else {
			page -= 1;
		}
		if (size == null) {
			size = 10;
		}
		// 按id降序排列
		Sort sort = new Sort(Direction.DESC, "id");
		PageRequest req = new PageRequest(page, size, sort);
		Page<Leave> p = lr.findAll(new BaseSpecification<Leave>(e),req);
		model.addAttribute("page", p);
		model.addAttribute("search", e);

	}

	/**
	 * 获取实体添加,编辑接口
	 * 
	 * @param obj
	 * @param model
	 * @return
	 */
	@GetMapping(value = "{id}")
	@PreAuthorize("hasPermission('sys:leave',1)")
	public String get(Leave obj, Model model) {
		if (obj.getId() != 0)
			obj = lr.findOne(obj.getId());
		model.addAttribute("obj", obj);

		return "leave/form";
	}

	/**
	 * 請假添加和编辑
	 * 
	 * @param target
	 * @return
	 * @throws IOException
	 */
	@PostMapping(value = "{id}")
	@Transactional
	@PreAuthorize("hasPermission('sys:leave',#target.id==0?8:4)")
	public String index(Leave target,HttpServletRequest request) throws IOException {

			// 添加
			if (target.getId() == 0) {
				
				//关联关系
				Integer employeeId=Integer.parseInt(request.getParameter("employeeId"));
				Integer employeeId2=Integer.parseInt(request.getParameter("employeeId2"));
				Integer departmentId=Integer.parseInt(request.getParameter("departmentId"));
				
				Employee e1=er.findOne(employeeId);
				Employee e2=er.findOne(employeeId2);
				Department d=dr.findOne(departmentId);
				
				target.setEmployee4(e1);
				target.setEmployee5(e2);
				target.setDepartment5(d);
				
				lr.save(target);

				return "redirect:index";
			}
			// 编辑
			else {
				Leave leave = lr.findOne(target.getId());
				if (leave == null) {
					return null;
				}

				//关联关系
				Integer employeeId=Integer.parseInt(request.getParameter("employeeId"));
				Integer employeeId2=Integer.parseInt(request.getParameter("employeeId2"));
				Integer departmentId=Integer.parseInt(request.getParameter("departmentId"));
				
				Employee e1=er.findOne(employeeId);
				Employee e2=er.findOne(employeeId2);
				Department d=dr.findOne(departmentId);
				
				target.setEmployee5(e2);
				target.setDepartment5(d);
				
				leave.setEmployee4(e1);
				leave.setEmployee5(e2);
				
				leave.setDepartment5(d);
				
				leave.setReason(target.getReason());
				leave.setCheckTime(target.getCheckTime());
				leave.setState(target.getState());
				

			}
		return "redirect:index";
	}

	/**
	 * 删除請假信息
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping(value = "{id}/delete")
	@Transactional
//	@PreAuthorize("hasPermission('sys:leave',16)")
	public String delete(@PathVariable("id") Integer id) {
		lr.findOne(id).setDelFlag(true);
		return "redirect:../index";
	}
	

}
